var MY_DOCTOR_VERIFY_LIST = (function() {

	function loadItem(){
		var params = getParams();
		// 输入有误的情况下，退出
		if (!params){
			return false;
		}
		
		var $tbodySpecimenList = $('#tbodySpecimenList');
		$tbodySpecimenList.empty();
		
		WeKnow.loading();
		
		$.ajax({
	        type: "POST", 
	        url: "invs_doctor_verify.do/load_list", 
	        dataType: "json",
	        contentType:"application/json",
	        data: JSON.stringify(params),
	        async: true,//异步调用
	        success: function(data) {
	        	
	        	WeKnow.closeLoading();
	        	
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	
	        	if (data.errcode == 0) {
	        		// 把取到的结果放到
	        		var e = '';
	        		
	        		if ( 'datalist' in data && data.datalist.length == 0 ){
	        			WeKnow.errorMsg('未查询到数据');
	        			return;
	        		}
	        		
	        		$(data.datalist).each(function( idx,_a){
	        			e+='<tr class="pointer" ';
	        				e+= ' data-doctor_id="' + _a.doctor.id + '"';
	        				e+= ' data-doctor_name="' + _a.doctor.name + '"';
		        			e+= ' data-institution_id="' + _a.institutionId + '"';
	        			e+='>';
		        			// 医师姓名
	        				e+='<td class="text-left">'+  _a.doctor.name + '</td>';
        					// 所在机构
	        				e+='<td class="text-left">'+  _a.institutionName + '</td>';
        					// 提交时间
	        				var submissionTm = '';
	        				if ( _a.doctor.submissionTm != null ){
	        					submissionTm = new Date(_a.doctor.submissionTm).format('yyyy-MM-dd hh:mm')
	        				}
	        				
	        				e+='<td class="text-center">'+ submissionTm + '</td>';
	        				
	        				// 审核中
	        				if ( _a.doctor.doctorVerifySt == 15 ){
	        					// 状态
		        				e+='<td class="text-center">未审核</td>';
	        					// 操作
		        				e+='<td class="text-center"><button type="button" name="verify" class="btn btn-default btn-sm">进行审核</button></td>';
	        				} else {
	        					// 状态
		        				e+='<td class="text-center">'+ (_a.doctor.doctorVerifySt == 20?'通过':'不通过') + '</td>';
	        					// 操作
		        				e+='<td class="text-center">';
		        					e+='<button type="button" name="showDetail" class="btn btn-default btn-sm">查看详细</button>';
//		        					e+='<button type="button" name="download" class="btn btn-default btn-sm">下载</button>';
		        				e+='</td>';
	        				}
	        			e+='</tr>';
	        		});
	        		$tbodySpecimenList.append(e);
	        		
	        		$('span#noVerifyCnt').text(data.noVerifyCnt);//未审核条数
	        		$('span#verifyCnt').text(data.verifiedCnt);//已审核条数
	        		
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( data.errMsg );
	        	} else {
	        		WeKnow.systemMsg('查询失败，请重新登录后再试');
	        	}
	        }
		});
	}
	
	// 查询单个医生
	function loadOneDoctor(doctorId,institutionId){
		
		// 明细页初期始化
		var $allInput = $('#doctor_verify_detail').find('input');
		$allInput.each(function(idx,_a){
			$(_a).val('');//输入框初始化
		});
		var $allSel = $('#doctor_verify_detail').find('select');
		$allSel.each(function(idx,_a){
			$(_a).val('-1');//选择框初始化
		});
		WeKnow.loading();
		
		var params = {};
		params.doctorId=doctorId;
		params.institutionId=institutionId;
		
		$.ajax({
	        type: "POST", 
	        url: "invs_doctor_verify.do/load_one_doctor", 
//	        dataType: "json",
//	        contentType:"application/json",
	        data: params,
	        async: true,//异步调用
	        success: function(data) {
	        	
	        	WeKnow.closeLoading();
	        	
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	
	        	if (data.errcode == 0) {
	        		
	        		var _a = data.doctor;
	        		// 显示结果
	        		$('#name').val( _a.name);
	        		$('#id_card').val( _a.idCard==null?'':_a.idCard);
	        		$('#phone').val( _a.phone==null?'':_a.phone);
	        		$('#qualification').val( _a.qualification==null?'':_a.qualification);
	        		$('#doctor_licence').val( _a.doctorLicence==null?'':_a.doctorLicence);
	        		if (_a.technicalTitle){
	        			$('#technical_title').val(_a.technicalTitle);
	        		}
	        		
	        		if (_a.scopePractice){
	        			$('#scope_practice').val(_a.scopePractice);
	        		}
	        		// 页面切换
	        		$('.verify-toggle').toggle();
	        		
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( data.errMsg );
	        	} else {
	        		WeKnow.systemMsg('查询失败，请重新登录后再试');
	        	}
	        }
		});
	}
	
	function getParams(){
		var params = {};
		
		params.doctorVerifySt = $('#doctorVerifySt').val();//审核状态
		var institutionName = $.trim( $('#institutionName').val());// 机构名
		if ( institutionName.length ){
			params.institutionName = institutionName;
		}
		params.queryYm = $('#year').val() + ''+$('#month').val();//年月
		return params;
	}
	
	return {
		loadItem:loadItem,
		loadOneDoctor:loadOneDoctor
	}
})();

$(document).ready(function() {
	
	MY_DOCTOR_VERIFY_LIST.loadItem();
	//查询按钮按下
	$('#search').on('click',function(){
		MY_DOCTOR_VERIFY_LIST.loadItem();
	});
	var ACTIVIE_CLASS = 'active_report_tab';
	
	// 设备消毒查询 、场所消毒查询
	var ACTIVIE_CLASS = 'active_tab';
	$('.report_tab').on('click',function(){
		var $that = $(this);
		
		var activeTabId = $that.prop('id');
		// 切换tab选中状态
		$('.active_tab').removeClass(ACTIVIE_CLASS);
		$that.addClass(ACTIVIE_CLASS);
		
		if ( activeTabId=='noVerify' ){
			$('#doctorVerifySt').val(15);
			
			$('#verified-search-key').hide();
		} else {
			$('#doctorVerifySt').val(20);
			
			$('#verified-search-key').show();
		}
		
		MY_DOCTOR_VERIFY_LIST.loadItem();
	});
	
	// 进行审核按钮按下
	$('tbody#tbodySpecimenList').on('click','button[name="verify"]',function(){
		var $that = $(this);
		
		var $parentTr = $that.closest('tr');
		
		var doctorId = $parentTr.data('doctor_id');
		var doctorName = $parentTr.data('doctor_name');
		var institutionId = $parentTr.data('institution_id');
		//显示医生名字
		$('#doctor_name_dis').text(doctorName);
		
		MY_DOCTOR_VERIFY_LIST.loadOneDoctor(doctorId,institutionId);
	});
	
	// 回列表页
	$('#backToWorkspace').on('click',function(){
		// 页面切换
		$('.verify-toggle').toggle();
	});
});